package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.group.network.express_plus_app.bean.MyResponse;
import com.example.group.network.express_plus_app.http.MySingleton;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class RegisterCourierActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    RequestQueue queue;
    EditText et_real_name;
    EditText et_id_card;
    EditText et_current_address;
    Spinner sp_sex;
    String server_ip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_courier);
        server_ip = getApplicationContext().getString(R.string.server_ip);
        queue= MySingleton.getInstance(getApplicationContext()).
                getRequestQueue();
        setActionBar("注册快递员");
        et_real_name=findViewById(R.id.et_real_name);
        et_id_card=findViewById(R.id.et_id_card);
        et_current_address=findViewById(R.id.et_current_address);
        initSpinner();

        findViewById(R.id.btn_register_courier).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerCourier();
            }
        });



    }

    private String[] sex={"男","女"};
    private void initSpinner(){
        ArrayAdapter<String> sexAdapter=new ArrayAdapter<String>(this,R.layout.item_select,sex);
        sexAdapter.setDropDownViewResource(R.layout.item_dropdown);

        sp_sex = (Spinner) findViewById(R.id.sp_sex);
        sp_sex.setPrompt("请选择您的性别");
        sp_sex.setAdapter(sexAdapter);
        sp_sex.setSelection(0);
        sp_sex.setOnItemSelectedListener(this);
    }


    public void registerCourier(){
        HashMap<String, String> mInfoMap = MainApplication.getInstance().mInfoMap;
        String phone = mInfoMap.get("phone");
        String token= mInfoMap.get("token");
        Log.i("phone",phone);
        Log.i("token",token);
        String real_name = et_real_name.getText().toString();
        String sex = sp_sex.getSelectedItem().toString();
        String id_card = et_id_card.getText().toString();
        String current_address = et_current_address.getText().toString();
        String register_courier_url_2=server_ip+"express-user/user-controller/common-user-register-courier?realName="
                +real_name+"&idCard="+id_card+"&familyAddress="+current_address+"&userSex="+sex+"&userPhone="+phone;

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.POST, register_courier_url_2, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        MyResponse myResponse = JSON.parseObject(response.toString(), MyResponse.class);

                        if (myResponse.getOk()){
                            //更新下数据库用户常用地址
                            showToast("快递员注册成功，您可以帮取快递赚钱了");
                        }else {
                            showToast("请检查输入内容！");
                        }
                        //tv_volley_data.setText(tv_volley_data.getText()+",Response: " + response.toString());
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: Handle error
                        showToast("请检查网络，连接超时");
                        //tv_volley_data.setText("Response: 连接超时" + error.getNetworkTimeMs());
                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> head=new HashMap<>();
                head.put("Authorization","Bearer "+token);
                return head;
            }
        };

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(6000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(jsonObjectRequest);
    }

    public void setActionBar(String title){
        //标题栏设置
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        supportActionBar.setTitle(title);

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void showToast(String desc) {
        Toast.makeText(getApplicationContext(), desc, Toast.LENGTH_SHORT).show();
    }

    //下来框选择监听器
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        sp_sex.setSelection(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}